﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using RH.Web.Utility;

namespace RH.Web
{
    #region Default Generated Global.asax
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    //public class MvcApplication : System.Web.HttpApplication
    //{
    //    protected void Application_Start()
    //    {
    //        AreaRegistration.RegisterAllAreas();

    //        WebApiConfig.Register(GlobalConfiguration.Configuration);
    //        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    //        RouteConfig.RegisterRoutes(RouteTable.Routes);
    //        BundleConfig.RegisterBundles(BundleTable.Bundles);
    //        AuthConfig.RegisterAuth();
    //    }
    //}
    #endregion

    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        //public static void RegisterRoutes(RouteCollection routes)
        //{
        //    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //    var nameFormat = @"[\-\w\d]+";
        //    //var actionFormat = @"\w+";

        //    routes.MapRoute(
        //        AppConstants.RouteNames.Public.ITEM_DETAILS,
        //        "product/details/{id}/{productKey}",
        //        new { action = "Details", controller = "Product", id = UrlParameter.Optional },
        //        new { productKey = nameFormat },
        //        new String[] { "RH.Web.Controllers" }
        //    );
        //    routes.MapRoute(
        //        AppConstants.RouteNames.Public.COMPANY_DETAILS,
        //        "{CompanyKey}",
        //        new { controller = "Company", action = "Details" },
        //        new { CompanyKey = String.Format("({0})", ApplicationCache.CompanyRoutes) },
        //        new String[] { "RH.Web.Controllers" }
        //    );
        //    routes.MapRoute(
        //        AppConstants.RouteNames.Public.COMPANY_PRODUCT_DETAILS,
        //        "{CompanyKey}/{id}/{productKey}",
        //        //new { controller = "Company", action = "ProductDetails" },
        //        new { controller = "Product", action = "Details" },
        //        new { CompanyKey = String.Format("({0})", ApplicationCache.CompanyRoutes), ProductKey = nameFormat },
        //        new String[] { "RH.Web.Controllers" }
        //    );
        //    routes.MapRoute(
        //        AppConstants.RouteNames.Public.CONTENT_DETAILS,
        //        "{ContentUrl}",
        //        new { controller = "Content", action = "ViewContent" },
        //        new { ContentUrl = String.Format("({0})", ApplicationCache.ContentRoutes) },
        //        new String[] { "RH.Web.Controllers" }
        //    );
        //    routes.MapRoute(
        //        AppConstants.RouteNames.Public.DEFAULT, // Route name
        //        "{controller}/{action}/{id}", // URL with parameters
        //        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        //        new String[] { "RH.Web.Controllers" }
        //    );
        //}

        //protected void Application_Start()
        //{
        //    //AreaRegistration.RegisterAllAreas();

        //    RegisterGlobalFilters(GlobalFilters.Filters);
        //    RefreshRoutes(false);
        //    //RegisterRoutes(RouteTable.Routes);
        //    //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
        //    ///Configure Auto Mapper
        //    RH.Web.Utility.BootStrapper.ConfigureTypeMappings();

        //    RH.Web.Utility.BootStrapper.SetUserRoles();
        //    RH.Web.Utility.BootStrapper.PrepareDomainUtility();
        //    RH.Common.Mail.MailManager.SmtpHost = RH.Web.Utility.ConfigReader.SmtpHost;
        //    RH.Common.Mail.MailManager.SmtpPort = RH.Web.Utility.ConfigReader.SmtpPort;
        //    //RH.Web.Utility.LuceneEngine.LuceneIndexDirectory = RH.Web.Utility.ConfigReader.LuceneIndexDirectory;
        //    //RH.Web.Utility.LuceneEngine.BuildSearchIndex();
        //}

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //AuthConfig.RegisterAuth();

            ///Configure Auto Mapper
            RH.Web.Utility.BootStrapper.ConfigureTypeMappings();

            RH.Web.Utility.BootStrapper.SetUserRoles();
            RH.Web.Utility.BootStrapper.PrepareDomainUtility();
            RH.Common.Mail.MailManager.SmtpHost = RH.Web.Utility.ConfigReader.SmtpHost;
            RH.Common.Mail.MailManager.SmtpPort = RH.Web.Utility.ConfigReader.SmtpPort;
        }
        protected void Application_Error(Object sender, EventArgs e)
        {
            BootStrapper.SendErrorEmail(Server.GetLastError());
        }

        public static void RefreshRoutes(bool forceRebuild)
        {
            if (RouteTable.Routes.Count > 0)
                RouteTable.Routes.Clear();

            ///Prepare the cache of URL Routes
            ApplicationCache.RefreshContentRouteCache(forceRebuild);
            ApplicationCache.RefreshCompanyRouteCache(forceRebuild);

            AreaRegistration.RegisterAllAreas();
            //RegisterRoutes(RouteTable.Routes);
        }
    }
}